package com.springboot.onlinemusicserver.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.springboot.onlinemusicserver.mapper.MusicLoveMapper;
import com.springboot.onlinemusicserver.mapper.MusicMapper;
import com.springboot.onlinemusicserver.model.LoginUser;
import com.springboot.onlinemusicserver.model.Music;
import com.springboot.onlinemusicserver.model.MusicLove;
import com.springboot.onlinemusicserver.service.MusicLoveService;
import com.springboot.onlinemusicserver.utils.common.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.springboot.onlinemusicserver.utils.common.SessionAttribute.USERINFO;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-05-26
 * Time: 9:24
 */
@Slf4j
@Service
public class MusicLoveServiceImp implements MusicLoveService {

    @Resource
    private MusicLoveMapper musicLoveMapper;

    @Resource
    private MusicMapper musicMapper;

    @Override
    public Result<Boolean> collectMusicLove(Long musicId, HttpServletRequest request) {
        // 1、检验参数是否合法
        if (musicId == null || musicId <= 0) {
            log.info("[addMusicLove]: 参数不合法");
            return Result.error("参数不合法", false);
        }
        if (isNotExistsMusic(musicId)) {
            log.info("[addMusicLove]: 音乐不存在");
            return Result.error("音乐不存在", false);
        }
        LoginUser loginUser = (LoginUser) request.getSession(false).getAttribute(USERINFO);
        Long userId = loginUser.getUserId();
        // 2、查询数据库看是否已经收藏过了
        LambdaQueryWrapper<MusicLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MusicLove::getMusicId, musicId).eq(MusicLove::getUserId, userId);
        List<MusicLove> musicLoveList = musicLoveMapper.selectList(queryWrapper);
        if (!musicLoveList.isEmpty()) {
            // 说明已经收藏过了
            log.info("[addMusicLove]: 该用户已经收藏过该音乐了");
            return Result.error("该用户已经收藏过该音乐了", false);
        }
        // 往数据库中插入数据
        MusicLove musicLove = new MusicLove();
        musicLove.setMusicId(musicId);
        musicLove.setUserId(userId);
        int result = musicLoveMapper.insert(musicLove);
        if (result != 1) {
            log.info("[addMusicLove]: 收藏音乐失败");
            return Result.error("收藏音乐失败", false);
        }
        log.info("[addMusicLove]: 收藏音乐成功");
        return Result.success(true);
    }

    private boolean isNotExistsMusic(Long musicId) {
        LambdaQueryWrapper<Music> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Music::getMusicId, musicId);
        return musicMapper.selectList(queryWrapper).isEmpty();
    }


    @Override
    public Result<Boolean> cancelMusicLove(Long musicId, HttpServletRequest request) {
        // 1、检验参数是否合法
        if (musicId == null || musicId <= 0) {
            log.info("[addMusicLove]: 参数不合法");
            return Result.error("参数不合法", false);
        }
        if (isNotExistsMusic(musicId)) {
            log.info("[addMusicLove]: 音乐不存在");
            return Result.error("音乐不存在", false);
        }
        LoginUser loginUser = (LoginUser) request.getSession(false).getAttribute(USERINFO);
        Long userId = loginUser.getUserId();
        // 2、查询数据库看是否存在该记录
        LambdaQueryWrapper<MusicLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MusicLove::getMusicId, musicId).eq(MusicLove::getUserId, userId);
        List<MusicLove> musicLoveList = musicLoveMapper.selectList(queryWrapper);
        if (musicLoveList.isEmpty()) {
            // 说明并未收藏
            log.info("[addMusicLove]: 该用户并未收藏该音乐");
            return Result.error("该用户并未收藏该音乐", false);
        }
        // 删除数据库中的记录
        LambdaUpdateWrapper<MusicLove> updateWrapper = new LambdaUpdateWrapper<>();
        // update music_love set status = 1 where music_id = 1 and user_id = 1
        updateWrapper.set(MusicLove::getStatus, 1)
                .eq(MusicLove::getMusicId, musicId)
                .eq(MusicLove::getUserId, userId);
        int result = musicLoveMapper.update(updateWrapper);
        if (result != 1) {
            log.info("[addMusicLove]: 取消收藏音乐失败");
            return Result.error("取消收藏音乐失败", false);
        }
        log.info("[addMusicLove]: 取消收藏音乐成功");
        return Result.success(true);
    }

    @Override
    public Result<List<Music>> selectMusicLoveList(String selectName, HttpServletRequest request) {
        // 判断查询参数是否为空
        LoginUser loginUser = (LoginUser) request.getSession(false).getAttribute(USERINFO);
        Long userId = loginUser.getUserId();
        List<Music> musicList = null;
        LambdaQueryWrapper<MusicLove> queryWrapper = new LambdaQueryWrapper<>();
        if (selectName == null) {
            // 查询所有
            musicList = musicLoveMapper.selectLoveMusicAll(userId);
        } else {
            // 模糊查询
            musicList = musicLoveMapper.selectLoveMusicBySelectName(userId, selectName);
        }
        log.info("[selectMusicLoveList]: 查询收藏音乐成功");
        return Result.success(musicList);
    }
}
